//
//  DetailspageViewController.m
//  FiltrationSystem
//
//  Created by 123456 on 2017/8/4.
//  Copyright © 2017年 YTYangK. All rights reserved.
//

#import "DetailspageViewController.h"
#import "UIImage+GIF.h"
#import "DetailspageViewModel.h"
#import "GUAAlertView.h"
#import "DPasswordView.h"
#import "ModifyViewController.h"

@interface DetailspageViewController ()
{
    NSDictionary *DetailsDict;//请求字典
    NSDictionary *DetailsDict1;//请求字典
    DPasswordView*DPview;
    NSTimer *_timer;
}

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *WindmillBLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *StateH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *StateW;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *MiddleHLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *DownButtonW;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *DownButtonH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *DownDownButtonW;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *DownDownButtonH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *DownDownBUtton1T;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *DownDownBUtton2T;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *DownDownBUtton3T;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *MiddleViewT;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *DownViewT;



@property (weak, nonatomic) IBOutlet UILabel *label1;

@end

@implementation DetailspageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
 
    [self layout];
    
    self.ImieLabel.text =self.imie;
    self.view.backgroundColor = RGB(33, 38, 44, 1);
    self.Stateimage.layer.cornerRadius = 10;
    self.Stateimage.backgroundColor =RGB(181, 182, 182, 1);
    [self.Stateimage.layer setMasksToBounds:YES];
    
    UIButton *buttonItem = [[UIButton alloc]initWithFrame:CGRectMake(5, 0, 23, 23)];
    [buttonItem setBackgroundImage:[UIImage imageNamed:@"修改名称"] forState:UIControlStateNormal];


    [buttonItem addTarget:self action:@selector(OnRButton) forControlEvents:UIControlEventTouchUpInside];


    UIBarButtonItem *RItem = [[UIBarButtonItem alloc]initWithCustomView:buttonItem];
    self.navigationItem.rightBarButtonItem =RItem;
    
    
    _timer =  [NSTimer scheduledTimerWithTimeInterval:40 target:self selector:@selector(function:) userInfo:nil repeats:YES];
    [NSNC addObserver:self selector:@selector(reloadData:) name:@"Timer" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setupUnreadCount) name:MDC_SET_BADGE object:nil]; // 激活时，推送时


}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //开启定时器每隔40秒刷新一次
    [_timer setFireDate:[NSDate distantPast]];
    
    NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc]initWithString:@"0小时"];
    [str1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:21.0f] range:NSMakeRange(0,1)];
    self.Timerlabel.attributedText =str1;
    
    [self requestURLwithView];

    self.title = [NSUD objectForKey:@"device_name"];

}

-(void)function:(NSTimer *)timer
{
    [self requestURLwithView];
    
}


- (void)requestURLwithView{

    DetailsDict = @{@"token":[NSUD objectForKey:@"token"],@"imie":self.imie};
    
    [DetailspageViewModel requestWithUrlForDetailsIn:self andParam:DetailsDict success:^(NSDictionary *model) {
        
        NSLog(@"请求返回 ===== %@",model);
        
        //运行时间
        [self getOvertime:model[@"activityLife"]];
        //状态
        
        [self setstate:[model[@"state"]integerValue] setboxStatus:[model[@"boxStatus"]integerValue] setflowCount:model[@"flowCount"]];

        
    } failure:^(NSString *error) {
        
    }];
    

}

- (void)getOvertime:(NSString*)mStr
{
    long msec = [mStr longLongValue];
    
    NSLog(@"%ld",msec);
    

  
    NSInteger d = msec/60/60/24;
    NSInteger h = msec/60/60%24;
    //NSInteger  m = msec/1000/60%60;
    //NSInteger  s = msec/1000%60;
    
    NSString *_tStr = @"";
    NSString *_dStr = @"";
    NSString *_hStr = @"";
    
    
    
    
    _tStr = [NSString stringWithFormat:@"%ld天%ld小时",d,h];
    
    
    if (d > 0)
    {
        _dStr = [NSString stringWithFormat:@"%ld天",d];
    }
    if (h >= 0)
    {
        _hStr = [NSString stringWithFormat:@"%ld小时",h];
    }
  
    _tStr = [NSString stringWithFormat:@"%@%@",_dStr,_hStr];
    

    if (![_tStr isEqualToString:@""]) {
        NSString *len = [NSString stringWithFormat:@"%ld",d];
        NSString *len1 = [NSString stringWithFormat:@"%ld",h];
        
        NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc]initWithString:_tStr];
        
        [str1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:21.0f] range:NSMakeRange(0,len.length)];
        
        [str1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:21.0f] range:NSMakeRange(_dStr.length,len1.length)];
        
        self.Timerlabel.attributedText =str1;
    }

    
}







- (IBAction)SetPassword:(UIButton *)sender {
    
    
    GUAAlertView *v = [GUAAlertView alertViewWithTitle:@"提示"
                                               message:@"确定重置当前设备运行时间吗？"
                                           buttonTitle:@"继续"
                                   buttonTouchedAction:^{
                                       NSLog(@"button touched");
                                       
                                           
                                               DPview = [[NSBundle mainBundle]loadNibNamed:@"DPasswordView" owner:self options:nil][0];
                                               
                                               UIWindow *keyv=[[UIApplication sharedApplication] keyWindow];
                                               DPview.frame = CGRectMake(0, 0, keyv.size.width, keyv.size.height);
                                               DPview.imie = self.imie;
                                               
                                               [keyv addSubview:DPview];
                                               DPview.alpha =0.9;

                                   } dismissAction:^{
                                       NSLog(@"dismiss");
                                       
                                   }];
    [v show];
    
   
}






- (void)reloadData: (NSNotification *)sender{

    [self requestURLwithView];
    [NSNC removeObserver:self name:@"Timer" object:self];

}


-(void)OnRButton{

    ModifyViewController *M = [[ModifyViewController alloc]init];
    M.imie = self.imie;
    [self.navigationController pushViewController:M animated:YES];

}


//设备状态
-(void)setstate:(double)state setboxStatus:(double)boxStatus setflowCount:(NSString *)flowCount{
    
    
    
    NSString *imageStr;
    NSString *imageStr1;
    
    int i;
    
    if (boxStatus == 1) {
        
        self.Icon2.image = [UIImage imageNamed:@"信号亮"];

        
    if (state ==11) {

        self.Icon1.image = [UIImage imageNamed:@"开机亮"];
        self.Icon3.image = [UIImage imageNamed:@"压力灰"];
        self.Icon4.image = [UIImage imageNamed:@"电机灰"];
        self.Stateimage.backgroundColor = RGB(19, 139, 43, 1);
        
        self.StateLabel.text = @"运行正常";
        imageStr = @"正常状态.gif";
        imageStr1 = @"时间.gif";
        i=0;
        
    }else if (state ==12){
        self.Icon4.image = [UIImage imageNamed:@"电机灰"];
        
        self.Icon1.image = [UIImage imageNamed:@"开机亮"];

        NSString  *filePath1 = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"GIF压力.gif" ofType:nil];
        NSData  *imageData1 = [NSData dataWithContentsOfFile:filePath1];
        self.Icon3.image = [UIImage sd_animatedGIFWithData:imageData1];
        self.Stateimage.backgroundColor = RGB(230, 0, 47, 1);
        
        self.StateLabel.text = @"压力超压";
        imageStr = @"压力超压.gif";
        imageStr1 = @"时间.gif";
        self.label1.text = @"压力超压";
        i=0;
        
        
        
    }else if (state ==21){
        
        self.Icon1.image = [UIImage imageNamed:@"开机灰"];
        self.Icon3.image = [UIImage imageNamed:@"压力灰"];
        self.Icon4.image = [UIImage imageNamed:@"电机灰"];
        self.StateLabel.text = @"系统关机";
        self.Stateimage.backgroundColor = [UIColor grayColor];
        self.Stateimage.backgroundColor = RGB(181, 182, 182, 1);

        imageStr = @"电机停止.png";
        imageStr1 = @"时间灰.png";
        i=1;
        
        
    }else if (state ==22){
        self.Icon1.image = [UIImage imageNamed:@"开机灰"];
        self.Icon4.image = [UIImage imageNamed:@"电机灰"];
        NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"GIF压力.gif" ofType:nil];
        NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
        self.Icon3.image = [UIImage sd_animatedGIFWithData:imageData];
        
        
        self.StateLabel.text = @"系统关机,压力超压";
        self.Stateimage.backgroundColor = RGB(230, 0, 47, 1);

        imageStr = @"压力超压.gif";
        imageStr1 = @"时间灰.png";
        i=1;
        
    }else if (state ==31){
        
        self.Icon3.image = [UIImage imageNamed:@"压力灰"];
        self.Icon1.image = [UIImage imageNamed:@"开机亮"];

        NSString  *filePath1 = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"GIF电机.gif" ofType:nil];
        NSData  *imageData1 = [NSData dataWithContentsOfFile:filePath1];
        self.Icon4.image = [UIImage sd_animatedGIFWithData:imageData1];

        self.StateLabel.text = @"电机故障";
        self.Stateimage.backgroundColor = RGB(230, 0, 47, 1);

        imageStr = @"马达故障.gif";
        imageStr1 = @"时间.gif";
        i=0;
        
        
    }else if (state ==32){
        
        self.Icon1.image = [UIImage imageNamed:@"开机亮"];
        
        NSString  *filePath2 = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"GIF压力.gif" ofType:nil];
        NSData  *imageData2 = [NSData dataWithContentsOfFile:filePath2];
        self.Icon3.image = [UIImage sd_animatedGIFWithData:imageData2];
        
        NSString  *filePath3 = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"GIF电机.gif" ofType:nil];
        NSData  *imageData3 = [NSData dataWithContentsOfFile:filePath3];
        self.Icon4.image = [UIImage sd_animatedGIFWithData:imageData3];
        
        self.StateLabel.text = @"电机故障,压力超压";
        self.Stateimage.backgroundColor = RGB(230, 0, 47, 1);
        
        imageStr = @"全部报警.gif";
        imageStr1 = @"时间.gif";
        i=0;
        
        
    }else if (state ==41){
        
        self.Icon3.image = [UIImage imageNamed:@"压力灰"];
        self.Icon1.image = [UIImage imageNamed:@"开机亮"];

        
        NSString  *filePath1 = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"GIF电机.gif" ofType:nil];
        NSData  *imageData1 = [NSData dataWithContentsOfFile:filePath1];
        self.Icon4.image = [UIImage sd_animatedGIFWithData:imageData1];
        
        self.StateLabel.text = @"电机缺相";
        self.Stateimage.backgroundColor = RGB(230, 0, 47, 1);

        imageStr = @"马达故障.gif";
        imageStr1 = @"时间.gif";
        i=0;
        
        
    }else if (state ==42){
        self.Icon1.image = [UIImage imageNamed:@"开机亮"];
        NSString  *filePath1 = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"GIF压力.gif" ofType:nil];
        NSData  *imageData1 = [NSData dataWithContentsOfFile:filePath1];
        self.Icon3.image = [UIImage sd_animatedGIFWithData:imageData1];        NSString  *filePath2 = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"GIF电机.gif" ofType:nil];
        NSData  *imageData2 = [NSData dataWithContentsOfFile:filePath2];
        self.Icon4.image = [UIImage sd_animatedGIFWithData:imageData2];
        
        self.Stateimage.backgroundColor = RGB(230, 0, 47, 1);

        self.StateLabel.text = @"电机缺相,压力超压";
        
        imageStr = @"全部报警.gif";
        imageStr1 = @"时间.gif";
        i=0;
        
        
        }else{
        
        self.Icon1.image = [UIImage imageNamed:@"电机灰"];
        self.Icon3.image = [UIImage imageNamed:@"压力灰"];
        self.Icon4.image = [UIImage imageNamed:@"电机灰"];
        self.Stateimage.backgroundColor = RGB(181, 182, 182, 1);

        self.StateLabel.text = @"设备离线";
        
        imageStr = @"电机停止.png";
        imageStr1 = @"时间灰.png";
        i=1;
        
        }
    
    }else{
    
        self.Icon1.image = [UIImage imageNamed:@"开机灰"];
        self.Icon2.image = [UIImage imageNamed:@"信号灰"];
        self.Icon3.image = [UIImage imageNamed:@"压力灰"];
        self.Icon4.image = [UIImage imageNamed:@"电机灰"];
        self.Stateimage.backgroundColor = RGB(181, 182, 182, 1);
        
        self.StateLabel.text = @"设备离线";
        imageStr = @"电机停止.png";
        imageStr1 = @"时间灰.png";
        i=1;
    
    }
    
    
    
    //头图
    NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:imageStr ofType:nil];
    
    NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
    self.TopImageview.image = [UIImage sd_animatedGIFWithData:imageData];
    
    //运行时间
    NSString  *filePath1 = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:imageStr1 ofType:nil];
    
    NSData  *imageData1 = [NSData dataWithContentsOfFile:filePath1];
    self.TimerImageView.image = [UIImage sd_animatedGIFWithData:imageData1];
    
    //流量计
    if (![flowCount isEqualToString:@""]) {
        
        if (i==1) {
            self.GearImageView.image = [UIImage imageNamed:@"齿轮灰"];
            
            NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"0L/min"]];
            [str1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:21.0f] range:NSMakeRange(0,1)];
            self.Geatlabel.attributedText =str1;
            
        }else{
            long len = [flowCount length];
            
            NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@L/min",flowCount]];
            [str1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:21.0f] range:NSMakeRange(0,len)];
            self.Geatlabel.attributedText =str1;
            
            NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"齿轮.gif" ofType:nil];
            
            NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
            self.GearImageView.image = [UIImage sd_animatedGIFWithData:imageData];
        }
    }else{
        self.Geatlabel.text = @"";
        self.GearImageView.image = [UIImage imageNamed:@"齿轮灰"];
    }
    
}


-(void)setupUnreadCount{

    [self requestURLwithView];

}



-(void)layout{

    if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"568.0")) {
        self.WindmillBLayout.constant = 230;
        self.StateH.constant = 35;
        self.StateW.constant = 35;
        self.DownButtonH.constant = 120;
        self.DownButtonW.constant = 95;
        self.DownDownButtonH.constant = 50;
        self.DownDownButtonW.constant = 45;
        self.DownDownBUtton1T.constant = -65;
        self.DownDownBUtton2T.constant = -65;
        self.DownDownBUtton3T.constant = -65;
        self.MiddleViewT.constant = 10;
        self.DownViewT.constant = 10;
        self.MiddleHLayout.constant = 50;

    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"667.0")){
        self.WindmillBLayout.constant = 290;
        self.MiddleHLayout.constant = 70;

        
    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"812.0")){
        
        
        self.WindmillBLayout.constant = 380;
        self.StateH.constant = 50;
        self.StateW.constant = 50;
        self.DownButtonH.constant = 150;
        self.DownButtonW.constant = 120;
        self.DownDownButtonH.constant = 80;
        self.DownDownButtonW.constant = 75;
        self.DownDownBUtton1T.constant = -90;
        self.DownDownBUtton2T.constant = -90;
        self.DownDownBUtton3T.constant = -90;
        self.MiddleHLayout.constant = 100;
        self.DownViewT.constant = -40;

    }else{
        self.WindmillBLayout.constant = 330;
        self.StateH.constant = 50;
        self.StateW.constant = 50;
        self.DownButtonH.constant = 150;
        self.DownButtonW.constant = 125;
        self.DownDownButtonH.constant = 80;
        self.DownDownButtonW.constant = 75;
        self.DownDownBUtton1T.constant = -90;
        self.DownDownBUtton2T.constant = -90;
        self.DownDownBUtton3T.constant = -90;
        
        
        self.MiddleHLayout.constant = 100;
        
    }


}





@end
